package com.zmn.plat.admin.controller.part.category.first;

import java.util.ArrayList;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.ResultDTO;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.admin.controller.part.category.second.PartCategorySecondModifyController;
import com.zmn.plat.model.entity.part.PartCategory;

/**
 * 类描述： PartCategoryFirstModifyController
 *
 * @author DaiZhichao
 * @since 2018/11/6 20:51
 */
@RestController
@RequestMapping("/part/category/first/ajax")
public class PartCategoryFirstModifyController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartCategoryFirstModifyController.class);
    @Resource
    PartCategorySecondModifyController partCategorySecondModifyController;

    /**
     * 描述:[Ajax] 一级分类添加/修改
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:51
     */
    @RequestMapping(value = "/addEdit", produces = "application/json; charset=utf-8")
    @RequiresPermissions("/part/category/first/ajax/addEdit.action")
    public ResponseDTO addEdit(@RequestBody PartCategory partCategory) {
        String lable = null;
        partCategory.setParentId(GlobalConsts.TOP_ID);
        if (partCategoryBService.findIdenticalCategory(partCategory)) {
            return ResponseDTO.fail("分类名称已存在，请勿重复添加");
        }

        if (Objects.isNull(partCategory.getCategId())) {
            partCategoryService.insert(partCategory, getMcStaff());
            lable = "新增配件分类";
        } else {
            partCategoryService.updateByKey(partCategory, getMcStaff());
            lable = "修改配件分类";
        }

        return ResponseDTO.success(String.format("%s成功！", lable));
    }

    /**
     * 描述: [Ajax]禁用一级分类
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:51
     */
    @RequestMapping("/disable")
    @RequiresPermissions("/part/category/first/ajax/updateStatus.action")
    public Object partCategoryDisable(HttpServletRequest request, int categId) {

        if (categId == 0) {
            return new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", true, "修改状态失败，CategId不能为空！");
        }

        Integer flag = partCategoryService.updateStatus(categId, (short)GlobalDict.STATUS_DISABLE, getMcStaffName(request));

        ResultDTO resultDTO;
        if (flag > 0) {
            resultDTO = new ResultDTO(StatusConsts.STATUS_SUCCESS, "OK", true, "修改状态成功！");
        } else {
            resultDTO = new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", true, "修改状态失败！");
        }
        return resultDTO;
    }

    /**
     * 描述: [Ajax] 启用一级分类
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:51
     */
    @RequestMapping("/enable")
    @RequiresPermissions("/part/category/first/ajax/updateStatus.action")
    public Object partCategoryEnable(HttpServletRequest request, int categId) {

        if (categId == 0) {
            return new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", true, "修改状态失败，CategId不能为空！");
        }

        Integer flag = partCategoryService.updateStatus(categId, (short)GlobalDict.STATUS_ENABLE, getMcStaffName(request));

        ResultDTO resultDTO;
        if (flag > 0) {
            resultDTO = new ResultDTO(StatusConsts.STATUS_SUCCESS, "OK", true, String.format("修改状态成功！"));
        } else {
            resultDTO = new ResultDTO(StatusConsts.STATUS_ERROR, "ERROR", true, String.format("修改状态失败！"));
        }
        return resultDTO;
    }

    @RequestMapping(value = "/updateStatus", produces = "application/json; charset=utf-8")
    @RequiresPermissions("/part/category/first/ajax/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids[]") ArrayList<Integer> ids, @RequestParam(value = "status") int status) {
        return partCategorySecondModifyController.updateStatus(request, response, ids, status);
    }
}
